﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;

namespace S7TCP
{
    public enum CPU_Type
    {
        S7200,
        S7200SMART,
        S7300 = 10,
        S7400 = 20
    }

    public enum DataType
    {
        Input = 129,
        Output,
        Marker,
        DataBlock,
        Timer = 29,
        Counter = 28
    }

    public enum ErrorCode
    {
        NoError,
        WrongCPU_Type,
        ConnectionError,
        IPAdressNotAvailable,
        WrongVarFormat = 10,
        WrongNumberReceivedBytes,
        SendData = 20,
        ReadData = 30,
        WriteData = 50
    }

    public enum VarType
    {
        Bit,
        Byte,
        Word,
        DWord,
        Int,
        DInt,
        Real,
        String,
        Timer,
        Counter
    }


    public class MyS7
    {
        //CPU类型
        public CPU_Type CPU { get; set; }

        //机架号
        public int Rack { get; set; }

        //插槽号
        public int Slot { get; set; }

        //IP地址
        public string Ip { get; set; }

        //Socket对象
        public Socket mSocket { get; set; }

        //连接成功标志位
        public bool IsConnected { get; set; }

        //字节数组
        public byte[] bReceive = new byte[512];

        #region 建立连接
        /// <summary>
        /// 建立与PLC的连接
        /// </summary>
        /// <param name="Ip">IP地址</param>
        /// <param name="Cpu">CPU类型</param>
        /// <param name="Rack">机架号</param>
        /// <param name="Slot">插槽号</param>
        /// <returns></returns>
        public Socket Connect(string Ip, CPU_Type Cpu, int Rack, int Slot)
        {
            //1、Sokcet对象连接
            try
            {
                this.mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                this.mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);
                this.mSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
                IPEndPoint ipe = new IPEndPoint(IPAddress.Parse(Ip), 102);
                this.mSocket.Connect(ipe);
            }
            catch (Exception)
            {
                this.IsConnected = false;
                return null;
            }

            //第一次握手
            try
            {
                byte[] array = new byte[]
				{
					3,
					0,
					0,
					22,
					17,
					224,
					0,
					0,
					0,
					46,
					0,
					193,
					2,
					1,
					0,
					194,
					2,
					3,
					0,
					192,
					1,
					9
				};
                switch (Cpu)
                {
                    case CPU_Type.S7200:
                        array[11] = 193;
                        array[12] = 2;
                        array[13] = 16;
                        array[14] = 0;
                        array[15] = 194;
                        array[16] = 2;
                        array[17] = 16;
                        array[18] = 0;
                        break;
                    case CPU_Type.S7200SMART:
                        array[9] = 1;
                        array[10] = 0;
                        array[11] = 193;
                        array[12] = 2;
                        array[13] = 1;
                        array[14] = 1;
                        array[15] = 194;
                        array[16] = 2;
                        array[17] = 1;
                        array[18] = 1;
                        array[19] = 192;
                        array[20] = 1;
                        array[21] = 10;
                        break;
                    case CPU_Type.S7300:
                        array[11] = 193;
                        array[12] = 2;
                        array[13] = 1;
                        array[14] = 0;
                        array[15] = 194;
                        array[16] = 2;
                        array[17] = 3;
                        array[18] = (byte)(Rack * 2 * 16 + Slot);
                        break;
                    case CPU_Type.S7400:
                        array[11] = 193;
                        array[12] = 2;
                        array[13] = 1;
                        array[14] = 0;
                        array[15] = 194;
                        array[16] = 2;
                        array[17] = 3;
                        array[18] = (byte)(Rack * 2 * 16 + Slot);
                        break;
                    default:
                        break;
                }

                this.mSocket.Send(array, 22, SocketFlags.None);


                int receive = this.mSocket.Receive(this.bReceive, 22, SocketFlags.None);
                if (receive != 22)
                {
                    throw new Exception();
                }
                //第二次握手
                byte[] buffer = new byte[]
				{
					3,
					0,
					0,
					25,
					2,
					240,
					128,
					50,
					1,
					0,
					0,
					255,
					255,
					0,
					8,
					0,
					0,
					240,
					0,
					0,
					3,
					0,
					3,
					1,
					0
				};
                switch (Cpu)
                {
                    case CPU_Type.S7200:
                        break;
                    case CPU_Type.S7200SMART:
                        buffer[11] = 204;
                        buffer[12] = 193;
                        buffer[20] = 1;
                        buffer[21] = 0;
                        buffer[22] = 1;
                        buffer[23] = 3;
                        buffer[24] = 192;
                        break;
                    case CPU_Type.S7300:
                        break;
                    case CPU_Type.S7400:
                        break;
                    default:
                        break;
                }
                this.mSocket.Send(buffer, 25, SocketFlags.None);
                //第三次握手
                int receive2 = this.mSocket.Receive(this.bReceive, 27, SocketFlags.None);
                if (receive2 != 27)
                {
                    throw new Exception();
                }
            }
            catch
            {
                this.IsConnected = false;
                return null;
            }
            this.IsConnected = true;
            return this.mSocket;

        }
        #endregion

        #region 读取指定存储区的指定长度字节
        /// <summary>
        /// 读取指定存储区的指定长度字节
        /// </summary>
        /// <param name="DataType"></param>
        /// <param name="DB"></param>
        /// <param name="StartByteAdr"></param>
        /// <param name="count"></param>
        /// <param name="mSocket"></param>
        /// <returns></returns>
        public byte[] ReadBytes(DataType DataType, int DB, int StartByteAdr, int count, Socket mSocket)
        {
            byte[] array = new byte[count];
            byte[] result;
            try
            {
                int num = 31;
                ByteArray byteArray = new ByteArray(num);
                ByteArray arg_24_0 = byteArray;
                byte[] array2 = new byte[3];
                array2[0] = 3;
                arg_24_0.Add(array2);
                byteArray.Add((byte)num);
                byteArray.Add(new byte[]
				{
					2,
					240,
					128,
					50,
					1,
					0,
					0,
					0,
					0,
					0,
					14,
					0,
					0,
					4,
					1,
					18,
					10,
					16
				});
                switch (DataType)
                {
                    case DataType.Counter:
                    case DataType.Timer:
                        byteArray.Add((byte)DataType);
                        break;
                    default:
                        byteArray.Add(2);
                        break;
                }
                byteArray.Add(Word.ToByteArray((ushort)count));
                byteArray.Add(Word.ToByteArray((ushort)DB));
                byteArray.Add((byte)DataType);
                byteArray.Add(0);
                switch (DataType)
                {
                    case DataType.Counter:
                    case DataType.Timer:
                        byteArray.Add(Word.ToByteArray((ushort)StartByteAdr));
                        break;
                    default:
                        byteArray.Add(Word.ToByteArray((ushort)(StartByteAdr * 8)));
                        break;
                }
                mSocket.Send(byteArray.array, byteArray.array.Length, SocketFlags.None);
                byte[] array3 = new byte[512];
                int num2 = mSocket.Receive(array3, 512, SocketFlags.None);
                if (array3[21] != 255)
                {
                    throw new Exception();
                }
                for (int i = 0; i < count; i++)
                {
                    array[i] = array3[i + 25];
                }
                result = array;
            }
            catch
            {
                this.IsConnected = false;
                result = null;
            }
            return result;
        }
        #endregion

        #region 读取指定的变量值
        public object Read(DataType DataType, int DB, int StartByteAdr, VarType VarType, int VarCount, Socket mSocket)
        {
            object result;
            switch (VarType)
            {
                case VarType.Byte:
                    {
                        int num = VarCount;
                        if (num < 1)
                        {
                            num = 1;
                        }
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = array[0];
                            }
                            else
                            {
                                result = array;
                            }
                        }
                        break;
                    }
                case VarType.Word:
                    {
                        int num = VarCount * 2;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = Word.FromByteArray(array);
                            }
                            else
                            {
                                result = Word.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.DWord:
                    {
                        int num = VarCount * 4;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = DWord.FromByteArray(array);
                            }
                            else
                            {
                                result = DWord.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.Int:
                    {
                        int num = VarCount * 2;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = Int.FromByteArray(array);
                            }
                            else
                            {
                                result = Int.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.DInt:
                    {
                        int num = VarCount * 4;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = DInt.FromByteArray(array);
                            }
                            else
                            {
                                result = DInt.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.Real:
                    {
                        int num = VarCount * 4;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = Double.BytetoFloatByPoint(array);
                            }
                            else
                            {
                                result = Double.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.String:
                    {
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, VarCount, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            result = String.FromByteArray(array);
                        }
                        break;
                    }
                case VarType.Timer:
                    {
                        int num = VarCount * 2;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = Timers.FromByteArray(array);
                            }
                            else
                            {
                                result = Timers.ToArray(array);
                            }
                        }
                        break;
                    }
                case VarType.Counter:
                    {
                        int num = VarCount * 2;
                        byte[] array = this.ReadBytes(DataType, DB, StartByteAdr, num, mSocket);
                        if (array == null)
                        {
                            result = null;
                        }
                        else
                        {
                            if (VarCount == 1)
                            {
                                result = Counter.FromByteArray(array);
                            }
                            else
                            {
                                result = Counter.ToArray(array);
                            }
                        }
                        break;
                    }
                default:
                    result = null;
                    break;
            }
            return result;
        }
        #endregion

        #region 根据变量地址读取数值
        public object Read(string variable, Socket mSocket)
        {
            string[] array = new string[8];
            string text = variable.ToUpper();
            text = text.Replace(" ", "");
            object result;
            try
            {
                string text2 = text.Substring(0, 2);
                switch (text2)
                {
                    case "DB":
                        {
                            string[] array2 = text.Split(new char[]
					{
						'.'
					});
                            if (array2.Length < 2)
                            {
                                throw new Exception();
                            }
                            int dB = int.Parse(array2[0].Substring(2));
                            string text3 = array2[1].Substring(0, 3);
                            int num2 = int.Parse(array2[1].Substring(3));
                            text2 = text3;
                            if (text2 != null)
                            {
                                if (text2 == "DBB")
                                {
                                    byte b = (byte)this.Read(DataType.DataBlock, dB, num2, VarType.Byte, 1, mSocket);
                                    result = b;
                                    return result;
                                }
                                if (text2 == "DBW")
                                {
                                    ushort num3 = (ushort)this.Read(DataType.DataBlock, dB, num2, VarType.Word, 1, mSocket);
                                    result = num3;
                                    return result;
                                }
                                if (text2 == "DBD")
                                {
                                    object obj = Math.Round(Convert.ToDouble(this.Read(DataType.DataBlock, dB, num2, VarType.Real, 1, mSocket)), 2);
                                    result = obj;
                                    return result;
                                }
                                if (text2 == "DBX")
                                {
                                    int startByteAdr = num2;
                                    int num4 = int.Parse(array2[2]);
                                    if (num4 > 7)
                                    {
                                        throw new Exception();
                                    }
                                    int num = Convert.ToInt32(Convert.ToString(Convert.ToInt32(this.Read(DataType.DataBlock, dB, startByteAdr, VarType.Byte, 1, mSocket)), 2));
                                    string text4 = num.ToString("0#######");
                                    array[num4] = text4.Substring(7 - num4, 1);
                                    result = array[num4];
                                    return result;
                                }
                            }
                            throw new Exception();
                        }
                    case "IB":
                        {
                            byte b2 = (byte)this.Read(DataType.Input, 0, int.Parse(text.Substring(2)), VarType.Byte, 1, mSocket);
                            result = b2;
                            return result;
                        }
                    case "IW":
                        {
                            ushort num5 = (ushort)this.Read(DataType.Input, 0, int.Parse(text.Substring(2)), VarType.Word, 1, mSocket);
                            result = num5;
                            return result;
                        }
                    case "ID":
                        {
                            uint num6 = (uint)this.Read(DataType.Input, 0, int.Parse(text.Substring(2)), VarType.DWord, 1, mSocket);
                            result = num6;
                            return result;
                        }
                    case "QB":
                        {
                            byte b2 = (byte)this.Read(DataType.Output, 0, int.Parse(text.Substring(2)), VarType.Byte, 1, mSocket);
                            result = b2;
                            return result;
                        }
                    case "QW":
                        {
                            ushort num5 = (ushort)this.Read(DataType.Output, 0, int.Parse(text.Substring(2)), VarType.Word, 1, mSocket);
                            result = num5;
                            return result;
                        }
                    case "QD":
                        {
                            uint num6 = (uint)this.Read(DataType.Output, 0, int.Parse(text.Substring(2)), VarType.DWord, 1, mSocket);
                            result = num6;
                            return result;
                        }
                    case "MB":
                        {
                            byte b2 = (byte)this.Read(DataType.Marker, 0, int.Parse(text.Substring(2)), VarType.Byte, 1, mSocket);
                            result = b2;
                            return result;
                        }
                    case "MW":
                        {
                            ushort num5 = (ushort)this.Read(DataType.Marker, 0, int.Parse(text.Substring(2)), VarType.Word, 1, mSocket);
                            result = num5;
                            return result;
                        }
                    case "MD":
                        {
                            uint num6 = (uint)this.Read(DataType.Marker, 0, int.Parse(text.Substring(2)), VarType.DWord, 1, mSocket);
                            result = num6;
                            return result;
                        }
                }
                text2 = text.Substring(0, 1);
                if (text2 != null)
                {
                    Dictionary<string, int> LIST = new Dictionary<string, int>(8)
						{
                        {"E",0},{"I",1},{"A",2},{"Q",3},{"M",4},{"T",5},{"Z",6},{"C",7}
						};

                    int num;
                    if (LIST.TryGetValue(text2, out num))
                    {
                        DataType dataType;
                        switch (num)
                        {
                            case 0:
                            case 1:
                                dataType = DataType.Input;
                                break;
                            case 2:
                            case 3:
                                dataType = DataType.Output;
                                break;
                            case 4:
                                dataType = DataType.Marker;
                                break;
                            case 5:
                                {
                                    double num7 = (double)this.Read(DataType.Timer, 0, int.Parse(text.Substring(1)), VarType.Timer, 1, mSocket);
                                    result = num7;
                                    return result;
                                }
                            case 6:
                            case 7:
                                {
                                    ushort num5 = (ushort)this.Read(DataType.Counter, 0, int.Parse(text.Substring(1)), VarType.Counter, 1, mSocket);
                                    result = num5;
                                    return result;
                                }
                            default:
                                goto IL_59A;
                        }
                        string text5 = text.Substring(1);
                        if (text5.IndexOf(".") == -1)
                        {
                            throw new Exception();
                        }
                        int startByteAdr = int.Parse(text5.Substring(0, text5.IndexOf(".")));
                        int num4 = int.Parse(text5.Substring(text5.IndexOf(".") + 1));
                        if (num4 > 7)
                        {
                            throw new Exception();
                        }
                        num = Convert.ToInt32(Convert.ToString(Convert.ToInt32(this.Read(dataType, 0, startByteAdr, VarType.Byte, 1, mSocket)), 2));
                        string text6 = num.ToString("0#######");
                        array[num4] = text6.Substring(7 - num4, 1);
                        result = array[num4];
                        return result;
                    }
                }
            IL_59A:
                throw new Exception();
            }
            catch
            {
                this.IsConnected = false;
                result = null;
            }
            return result;
        }
        #endregion

        #region 根据数据类型、DB号、起始地址写入数值
        public bool WriteBytes(DataType DataType, int DB, int StartByteAdr, byte[] value, Socket mSocket)
        {
            byte[] array = new byte[513];
            bool result;
            try
            {
                int num = value.Length;
                int num2 = 35 + value.Length;
                ByteArray byteArray = new ByteArray(num2);
                ByteArray arg_33_0 = byteArray;
                byte[] array2 = new byte[3];
                array2[0] = 3;
                arg_33_0.Add(array2);
                byteArray.Add((byte)num2);
                byteArray.Add(new byte[]
				{
					2,
					240,
					128,
					50,
					1,
					0,
					0
				});
                byteArray.Add(Word.ToByteArray((ushort)(num - 1)));
                byteArray.Add(new byte[]
				{
					0,
					14
				});
                byteArray.Add(Word.ToByteArray((ushort)(num + 4)));
                byteArray.Add(new byte[]
				{
					5,
					1,
					18,
					10,
					16,
					2
				});
                byteArray.Add(Word.ToByteArray((ushort)num));
                byteArray.Add(Word.ToByteArray((ushort)DB));
                byteArray.Add((byte)DataType);
                byteArray.Add(0);
                byteArray.Add(Word.ToByteArray((ushort)(StartByteAdr * 8)));
                byteArray.Add(new byte[]
				{
					0,
					4
				});
                byteArray.Add(Word.ToByteArray((ushort)(num * 8)));
                byteArray.Add(value);
                mSocket.Send(byteArray.array, byteArray.array.Length, SocketFlags.None);
                int num3 = mSocket.Receive(array, 512, SocketFlags.None);
                if (array[21] != 255)
                {
                    throw new Exception();
                }
                result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }
        #endregion

        #region 写入单个变量数值
        public bool Write(DataType DataType, int DB, int StartByteAdr, object value, Socket mSocket)
        {
            string name = value.GetType().Name;
            string name2 = value.GetType().Name;
            bool result;
            if (name2 != null)
            {
                Dictionary<string, int> list = new Dictionary<string, int>(13)
					{

						{
							"Byte",
							0
						},

						{
							"Int16",
							1
						},

						{
							"UInt16",
							2
						},

						{
							"Int32",
							3
						},

						{
							"UInt32",
							4
						},

						{
							"Double",
							5
						},

						{
							"Byte[]",
							6
						},

						{
							"Int16[]",
							7
						},

						{
							"UInt16[]",
							8
						},

						{
							"Int32[]",
							9
						},

						{
							"UInt32[]",
							10
						},

						{
							"Double[]",
							11
						},

						{
							"String",
							12
						}
					};

                int num;
                if (list.TryGetValue(name2, out num))
                {
                    byte[] value2;
                    switch (num)
                    {
                        case 0:
                            value2 = Byte.ToByteArray((byte)value);
                            break;
                        case 1:
                            value2 = Int.ToByteArray((short)value);
                            break;
                        case 2:
                            value2 = Word.ToByteArray((ushort)value);
                            break;
                        case 3:
                            value2 = DInt.ToByteArray((int)value);
                            break;
                        case 4:
                            value2 = DWord.ToByteArray((uint)value);
                            break;
                        case 5:
                            value2 = Double.ToByteArray((double)value);
                            break;
                        case 6:
                            value2 = (byte[])value;
                            break;
                        case 7:
                            value2 = Int.ToByteArray((short[])value);
                            break;
                        case 8:
                            value2 = Word.ToByteArray((ushort[])value);
                            break;
                        case 9:
                            value2 = DInt.ToByteArray((int[])value);
                            break;
                        case 10:
                            value2 = DWord.ToByteArray((uint[])value);
                            break;
                        case 11:
                            value2 = Double.ToByteArray((double[])value);
                            break;
                        case 12:
                            value2 = String.ToByteArray(value as string);
                            break;
                        default:
                            goto IL_1FB;
                    }
                    result = this.WriteBytes(DataType, DB, StartByteAdr, value2, mSocket);
                    return result;
                }
            }
        IL_1FB:
            result = false;
            return result;
        }
        #endregion

        #region 根据变量地址写入数值
        public bool Write(string variable, object value, Socket mSocket)
        {
            string text = variable.ToUpper();
            text = text.Replace(" ", "");
            bool result;
            try
            {
                string text2 = text.Substring(0, 2);
                switch (text2)
                {
                    case "DB":
                        {
                            string[] array = text.Split(new char[]
					{
						'.'
					});
                            if (array.Length < 2)
                            {
                                throw new Exception();
                            }
                            int dB = int.Parse(array[0].Substring(2));
                            string text3 = array[1].Substring(0, 3);
                            int num2 = int.Parse(array[1].Substring(3));
                            text2 = text3;
                            if (text2 != null)
                            {
                                if (text2 == "DBB")
                                {
                                    object obj = Convert.ChangeType(value, typeof(byte));
                                    result = this.Write(DataType.DataBlock, dB, num2, (byte)obj, mSocket);
                                    return result;
                                }
                                if (text2 == "DBW")
                                {
                                    object obj = Convert.ChangeType(value, typeof(ushort));
                                    result = this.Write(DataType.DataBlock, dB, num2, (ushort)obj, mSocket);
                                    return result;
                                }
                                if (text2 == "DBD")
                                {
                                    object obj = Convert.ChangeType(value, typeof(double));
                                    result = this.Write(DataType.DataBlock, dB, num2, (double)obj, mSocket);
                                    return result;
                                }
                                if (!(text2 == "DBX"))
                                {
                                    if (text2 == "DBS")
                                    {
                                        result = this.Write(DataType.DataBlock, dB, num2, (string)value, mSocket);
                                        return result;
                                    }
                                }
                                else
                                {
                                    int startByteAdr = num2;
                                    int num3 = int.Parse(array[2]);
                                    if (num3 > 7)
                                    {
                                        throw new Exception();
                                    }
                                    byte b = (byte)this.Read(DataType.DataBlock, dB, startByteAdr, VarType.Byte, 1, mSocket);
                                    if (Convert.ToInt32(value) == 1)
                                    {
                                        b |= (byte)Math.Pow(2.0, (double)num3);
                                    }
                                    else
                                    {
                                        b &= (byte)Math.Pow(2.0, (double)num3);
                                    }
                                    result = this.Write(DataType.DataBlock, dB, startByteAdr, b, mSocket);
                                    return result;
                                }
                            }
                            throw new Exception();
                        }
                    case "EB":
                        {
                            object obj = Convert.ChangeType(value, typeof(byte));
                            result = this.Write(DataType.Input, 0, int.Parse(text.Substring(2)), (byte)obj, mSocket);
                            return result;
                        }
                    case "EW":
                        {
                            object obj = Convert.ChangeType(value, typeof(ushort));
                            result = this.Write(DataType.Input, 0, int.Parse(text.Substring(2)), (ushort)obj, mSocket);
                            return result;
                        }
                    case "ED":
                        {
                            object obj = Convert.ChangeType(value, typeof(uint));
                            result = this.Write(DataType.Input, 0, int.Parse(text.Substring(2)), (uint)obj, mSocket);
                            return result;
                        }
                    case "AB":
                        {
                            object obj = Convert.ChangeType(value, typeof(byte));
                            result = this.Write(DataType.Output, 0, int.Parse(text.Substring(2)), (byte)obj, mSocket);
                            return result;
                        }
                    case "AW":
                        {
                            object obj = Convert.ChangeType(value, typeof(ushort));
                            result = this.Write(DataType.Output, 0, int.Parse(text.Substring(2)), (ushort)obj, mSocket);
                            return result;
                        }
                    case "AD":
                        {
                            object obj = Convert.ChangeType(value, typeof(uint));
                            result = this.Write(DataType.Output, 0, int.Parse(text.Substring(2)), (uint)obj, mSocket);
                            return result;
                        }
                    case "MB":
                        {
                            object obj = Convert.ChangeType(value, typeof(byte));
                            result = this.Write(DataType.Marker, 0, int.Parse(text.Substring(2)), (byte)obj, mSocket);
                            return result;
                        }
                    case "MW":
                        {
                            object obj = Convert.ChangeType(value, typeof(ushort));
                            result = this.Write(DataType.Marker, 0, int.Parse(text.Substring(2)), (ushort)obj, mSocket);
                            return result;
                        }
                    case "MD":
                        result = this.Write(DataType.Marker, 0, int.Parse(text.Substring(2)), value, mSocket);
                        return result;
                }
                text2 = text.Substring(0, 1);
                if (text2 != null)
                {
                    Dictionary<string, int> list = new Dictionary<string, int>(8)
						{

							{
								"E",
								0
							},

							{
								"I",
								1
							},

							{
								"A",
								2
							},

							{
								"O",
								3
							},

							{
								"M",
								4
							},

							{
								"T",
								5
							},

							{
								"Z",
								6
							},

							{
								"C",
								7
							}
						};

                    int num;
                    if (list.TryGetValue(text2, out num))
                    {
                        DataType dataType;
                        switch (num)
                        {
                            case 0:
                            case 1:
                                dataType = DataType.Input;
                                break;
                            case 2:
                            case 3:
                                dataType = DataType.Output;
                                break;
                            case 4:
                                dataType = DataType.Marker;
                                break;
                            case 5:
                                result = this.Write(DataType.Timer, 0, int.Parse(text.Substring(1)), (double)value, mSocket);
                                return result;
                            case 6:
                            case 7:
                                result = this.Write(DataType.Counter, 0, int.Parse(text.Substring(1)), (short)value, mSocket);
                                return result;
                            default:
                                goto IL_671;
                        }
                        string text4 = text.Substring(1);
                        if (text4.IndexOf(".") == -1)
                        {
                            throw new Exception();
                        }
                        int startByteAdr = int.Parse(text4.Substring(0, text4.IndexOf(".")));
                        int num3 = int.Parse(text4.Substring(text4.IndexOf(".") + 1));
                        if (num3 > 7)
                        {
                            throw new Exception();
                        }
                        byte b2 = (byte)this.Read(dataType, 0, startByteAdr, VarType.Byte, 1, mSocket);
                        if (Convert.ToInt32(value) == 1)
                        {
                            b2 |= (byte)Math.Pow(2.0, (double)num3);
                        }
                        else
                        {
                            b2 &= (byte)(b2 ^ (byte)Math.Pow(2.0, (double)num3));
                        }
                        result = this.Write(dataType, 0, startByteAdr, b2, mSocket);
                        return result;
                    }
                }
            IL_671:
                throw new Exception();
            }
            catch
            {
                result = false;
            }
            return result;
        }
        #endregion

    }
}
